package com.shaoshaohuo.app.ui.shipper;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.GrabDriver;
import com.shaoshaohuo.app.entity.GrabDriverEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.adapter.GrabOrderDriverListAdapter;
import com.shaoshaohuo.app.view.TopbarView;

/**
 * @ClassName SelectDriverActivity
 * @Decription 选择司机
 * @2015-8-5 上午12:27:11
 */
public class SelectDriverActivity extends BaseActivity {

	private TopbarView mTopbarView;
	
	private ListView mListView;
	
	private GrabOrderDriverListAdapter mAdapter;

	private String orderid;
	
	private TextView mStartCityText;
	private TextView mEndCityText;
	private TextView mCargoTypeText;

    private String endCity;

    private String cargoType;

    private String startCity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_driver);
		initData();
		initView();
		setUpView();
		
		requestData();
	}

	private void requestData() {
		startLoadingDialog();
		RequestService.getInstance().getQiangList(this, orderid, GrabDriverEntity.class, new RequestListener() {
			
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if(resultData.isOk()){
					GrabDriverEntity entity = (GrabDriverEntity) resultData;
					setAdapter(entity);
				}else{
					showToast(resultData.getMsg());
				}
			}
			
			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(msg);
			}
		});
	}

	protected void setAdapter(GrabDriverEntity entity) {
		if(mAdapter == null){
			mAdapter = new GrabOrderDriverListAdapter<>(this, entity.getData().getList(), false);
			mListView.setAdapter(mAdapter);
		}
	}

	private void initData() {
		orderid = getIntent().getStringExtra(ExtraName.orderid);
		startCity = getIntent().getStringExtra(ExtraName.startCity);
		endCity = getIntent().getStringExtra(ExtraName.endCity);
		cargoType = getIntent().getStringExtra(ExtraName.cargoType);
	}
	
	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

		mListView = (ListView) this.findViewById(R.id.listview_driver_list);
		
		mStartCityText = (TextView) this.findViewById(R.id.textview_start_city);
		mEndCityText = (TextView) this.findViewById(R.id.textview_end_city);
		mCargoTypeText = (TextView) this.findViewById(R.id.textview_cargo_type);
	}

	private void setUpView() {
		mTopbarView.setCenterText("选择司机");
		mTopbarView.setLeftView(true, true);

//		List<String> list = new ArrayList<>();
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//
//		GrabOrderDriverListAdapter<String> mAdapter = new GrabOrderDriverListAdapter<>(this, list, false);
//		mListView.setAdapter(mAdapter);
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				GrabDriver driver = (GrabDriver) parent.getItemAtPosition(position);
				showConfimDialog(driver);
			}
		});
		
		mStartCityText.setText(startCity);
		mEndCityText.setText(endCity);
		mCargoTypeText.setText(cargoType);
	}
	
	private void showConfimDialog(final GrabDriver driver) {
			AlertDialog.Builder builder  = new AlertDialog.Builder(this);
			builder.setMessage("确认选择 "+driver.getCaruserinfo().getRealname()+"?");
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					selectDriver(driver);
				}
			});
			builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			builder.create().show();
	}
	
	private void selectDriver(GrabDriver driver) {
		startLoadingDialog();
		RequestService.getInstance().selectGrabDriver(this, orderid, driver.getCaruserid(), BaseEntity.class, new RequestListener() {
			
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if(resultData.isOk()){
					Intent intent = new Intent(SelectDriverActivity.this,PayOrderActivity.class);
					intent.putExtra(ExtraName.orderid, orderid);
					startActivity(intent);
				}else{
					showToast(resultData.getMsg());
				}
			}
			
			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(msg);
			}
		});
		
	}
	
}
